package web.manager;

import bean.Manager;
import bean.Staff;
import bean.Video;
import com.alibaba.fastjson.JSON;
import entity.ManagerEntity;
import entity.StaffEntity;
import entity.VideoEntity;
import util.FANREN;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "Transfer", urlPatterns = "/dao/transfer")
public class Transfer extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        EntityManager em = (EntityManager) request.getAttribute("_entity.Manager");
        Manager manager = (Manager) request.getSession().getAttribute("manager");

        Query query = em.createQuery("select m from ManagerEntity m where m.username = " + manager.getUsername());
        List<ManagerEntity> list = query.getResultList();
        if (list.size() == 0) {
            response.sendRedirect("/pages/connect.html");
        } else {
            manager.setMid(list.get(0).getMid());
            manager.setPwd(list.get(0).getPwd());
            request.getSession().setAttribute("manager",manager);
            response.sendRedirect("/index.html");
        }
    }
}

